# 棋盘大小
BOARD_SIZE = 9

# 初始化棋盘
def initialize_board():
    board = [[' ' for _ in range(BOARD_SIZE)] for _ in range(BOARD_SIZE)]
    # 先手方棋子
    board[0] = ['飛', '角', '桂', '香', '王', '香', '桂', '角', '飛']
    board[1] = [' '] * 9
    board[2] = [' '] * 9
    board[3] = ['歩'] * 9

    # 后手方棋子
    board[5] = ['歩'] * 9
    board[6] = [' '] * 9
    board[7] = [' '] * 9
    board[8] = ['飛', '角', '桂', '香', '玉', '香', '桂', '角', '飛']
    return board

# 打印棋盘
def print_board(board):
    print("  1 2 3 4 5 6 7 8 9")
    for i in range(BOARD_SIZE):
        print(f"{i + 1}", end=" ")
        for j in range(BOARD_SIZE):
            print(board[i][j], end=" ")
        print(i + 1)
    print("  1 2 3 4 5 6 7 8 9")

# 检查坐标是否在棋盘内
def is_valid_coordinate(x, y):
    return 0 <= x < BOARD_SIZE and 0 <= y < BOARD_SIZE

# 简单的移动规则检查（这里只是简单示例，未涵盖所有规则）
def is_valid_move(board, from_x, from_y, to_x, to_y, current_player):
    piece = board[from_x][from_y]
    if piece == ' ':
        return False
    # 简单区分先手和后手棋子
    is_first_player = piece in ['飛', '角', '桂', '香', '王', '歩']
    if (current_player == '先手' and not is_first_player) or (current_player == '后手' and is_first_player):
        return False
    if from_x == to_x and from_y == to_y:
        return False
    if not is_valid_coordinate(to_x, to_y):
        return False
    # 简单示例：歩（兵）的移动规则
    if piece == '歩':
        if current_player == '先手':
            return to_x == from_x - 1 and to_y == from_y
        else:
            return to_x == from_x + 1 and to_y == from_y
    return True

# 移动棋子
def move_piece(board, from_x, from_y, to_x, to_y):
    piece = board[from_x][from_y]
    board[from_x][from_y] = ' '
    board[to_x][to_y] = piece
    return board

# 检查是否有一方获胜（简单判断王或玉被吃掉）
def check_winner(board):
    first_player_king = False
    second_player_king = False
    for i in range(BOARD_SIZE):
        for j in range(BOARD_SIZE):
            if board[i][j] == '王':
                first_player_king = True
            elif board[i][j] == '玉':
                second_player_king = True
    if not first_player_king:
        return '后手'
    elif not second_player_king:
        return '先手'
    return None

# 主游戏循环
def main():
    board = initialize_board()
    current_player = '先手'
    while True:
        print_board(board)
        print(f"当前玩家: {current_player}")
        try:
            from_pos = input("请输入要移动的棋子位置（例如：1 2）：").split()
            to_pos = input("请输入目标位置（例如：2 3）：").split()
            from_x, from_y = int(from_pos[0]) - 1, int(from_pos[1]) - 1
            to_x, to_y = int(to_pos[0]) - 1, int(to_pos[1]) - 1
            if is_valid_move(board, from_x, from_y, to_x, to_y, current_player):
                board = move_piece(board, from_x, from_y, to_x, to_y)
                winner = check_winner(board)
                if winner:
                    print_board(board)
                    print(f"{winner} 获胜！游戏结束。")
                    break
                current_player = '后手' if current_player == '先手' else '先手'
            else:
                print("无效的移动，请重新尝试。")
        except (ValueError, IndexError):
            print("输入无效，请输入有效的位置。")

if __name__ == "__main__":
    main()